package cn.qxnekoo.chain.service;

/**
 * @author tdf
 * @date 2020-05-25 15:34
 **/
public abstract class AbstractHandler {
	protected static final String TAG = AbstractHandler.class.getSimpleName();

	public AbstractHandler nextHandler;

	private int level = 0;//能处理的级别

	public AbstractHandler(int level) {
		this.level = level;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public void setNextHandler(AbstractHandler abstractHandler) {
		this.nextHandler = abstractHandler;
	}

	public final boolean handleRequest(AbstractRequest request) {

		if (handle(request)) {
			System.out.println("ok 结束了");
			return true;
		}else {
			if (nextHandler != null) {
				return this.nextHandler.handleRequest(request);
			}else {
				System.out.println("我处理不了了");
				return false;
				//当所有处理者对象均不能处理该请求时输出
			}
		}
	}

	protected abstract boolean handle(AbstractRequest request);
}
